// pages/home/home.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    productDetails: [],
    limit: [],
    skip: 0,
    num: 0,
    // 轮播图地址
    swiperList: [],
    //招商电话
    telephonenumber: "XXXXXXXXXXX",
    // 选项卡信息数组
    homeItem: [{
      // name: "newspaper-o",
      src: '../../images/news.png',
      id: 1,
      title: "旅游新闻"
    }, {
      // name: "fire-o",
      src: '../../images/country.png',
      id: 2,
      title: "乡村游"
    }, {
      // name: "send-gift-o",
      src: '../../images/shop.png',
      id: 3,
      title: "土特产"
    }, {
      // name: "flag-o",
      src: '../../images/simiao.png',
      id: 4,
      title: "寺院揽胜"
    }, {
      // name: "wap-home-o",
      src: '../../images/nongjiale.png',
      id: 5,
      title: "农家乐推荐"
    }]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getList('swiper')
      .then(res => {
        this.setData({
          swiperList: res.data
        })
        console.log('请求成功', res)
      })
      .catch(err => {
        console.log('请求失败', err)
      })
    this.getProduct(0)
  },
  // 跳转到相应模块
  gotoProduct(e) {
    let id = e.currentTarget.dataset.id
    if (id == 1) {
      wx.navigateTo({
        url: '../news/news',
      })
    } else if (id == 2) {
      wx.navigateTo({
        url: '../countryTour/countryTour',
      })
    } else if (id == 3) {
      wx.navigateTo({
        url: '../product/product',
      })
    } else if (id == 4) {
      wx.navigateTo({
        url: '../monastery/monastery',
      })
    } else if (id == 5) {
      wx.navigateTo({
        url: '../agritainment/agritainment',
      })
    }
  },
  // 跳转到广告模块
  goToAdDetail() {
    wx.navigateTo({
      url: '../adDetail/adDetail',
    })
  },
  //轮播图向详情跳转
  goToSwiperDetail(e) {
    if (e.currentTarget.dataset.type == "hotel") {
      wx.navigateTo({
        url: '../rooms/detail/detail?id=' + e.currentTarget.dataset.id,
      })
    } else if (e.currentTarget.dataset.type == "product") {
      wx.navigateTo({
        url: '../productDetail/productDetail?id=' + e.currentTarget.dataset.id,
      })
    }
  },
  //获取要在首页显示的土特产信息
  getProduct(num) {
    let db = wx.cloud.database().collection('product_details')
      .where({
        home: "true"
      })
    db.skip(this.data.skip).limit(4)
      .get()
      .then(res => {
        console.log('前skip:', this.data.skip)
        if (num == 0) {
          this.setData({
            productDetails: res.data,
            limit: res.data,
            skip: res.data.length
          })
        } else {
          this.setData({
            productDetails: this.data.productDetails.concat(res.data),
            limit: res.data,
            skip: this.data.productDetails.concat(res.data).length
          })
        }
        wx.hideLoading({
          success: (res) => {},
        })
        console.log('后skip:', this.data.skip)
        console.log('length:', this.data.productDetails.length)
        console.log('请求成功', res)
      })
      .catch(err => {
        wx.hideLoading({
          success: (res) => {},
        })
        console.log('请求失败', err)
      })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.limit.length < 4) {
      wx.showToast({
        title: '没有更多推荐商品了',
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '加载中'
      })
      this.getProduct(1)
    }
  },
  //跳转到详情页面
  goToDetail(e) {
    wx.navigateTo({
      url: '../productDetail/productDetail?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})